package com.ruoyi.information.service.back.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.information.domain.back.SysUserBack;
import com.ruoyi.information.mapper.back.SysUserBackMapper;
import com.ruoyi.information.mapper.back.SysUserNeedBackMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.information.domain.back.SysUserNeedBack;
import com.ruoyi.information.service.back.ISysUserNeedBackService;

/**
 * 需求方Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-08
 */
@Service
public class SysUserNeedBackServiceImpl implements ISysUserNeedBackService
{
    @Autowired
    private SysUserNeedBackMapper sysUserNeedMapper;

    @Autowired
    private SysUserBackMapper sysUserMapper;

    /**
     * 查询需求方
     *
     * @param userId 需求方主键
     * @return 需求方
     */
    @Override
    public SysUserNeedBack selectSysUserNeedByUserId(Long userId)
    {
        return sysUserNeedMapper.selectSysUserNeedByUserId(userId);
    }

    /**
     * 查询需求方列表
     *
     * @param sysUserNeed 需求方
     * @return 需求方
     */
    @Override
    public List<SysUserNeedBack> selectSysUserNeedList(SysUserNeedBack sysUserNeed)
    {
        return sysUserNeedMapper.selectSysUserNeedList(sysUserNeed);
    }

    /**
     * 新增需求方
     *
     * @param sysUserNeed 需求方
     * @return 结果
     */
    @Override
    public int insertSysUserNeed(SysUserNeedBack sysUserNeed)
    {
        SysUserBack sysUser = new SysUserBack();
        BeanUtils.copyProperties(sysUserNeed,sysUser);
        /*校验手机号*/
        if (sysUserMapper.checkPhoneNumber(sysUser) == 0) {


            sysUser.setCreateTime(DateUtils.getNowDate());
            sysUser.setPassword(SecurityUtils.encryptPassword(sysUserNeed.getPassword()));
            sysUser.setUserName(sysUser.getPhonenumber());
            sysUser.setUserType("04");
            sysUserMapper.insertSysUser(sysUser);
            sysUserMapper.insertUserRole(sysUser.getUserId(),103);


            sysUserNeed.setUserId(sysUser.getUserId());
            return sysUserNeedMapper.insertSysUserNeed(sysUserNeed);
        } else {
            throw new RuntimeException("手机号已存在");
        }
    }

    /**
     * 修改需求方
     *
     * @param sysUserNeed 需求方
     * @return 结果
     */
    @Override
    public int updateSysUserNeed(SysUserNeedBack sysUserNeed)
    {
        SysUserBack sysUser = new SysUserBack();
        BeanUtils.copyProperties(sysUserNeed,sysUser);

        SysUserBack user = sysUserMapper.selectSysUserByUserId(sysUser.getUserId());
        /*检查手机号是否修改*/
        if (sysUser.getPhonenumber() != null && user.getPhonenumber().equals(sysUser.getPhonenumber())) {
            sysUser.setUserName(sysUser.getPhonenumber());
            sysUserMapper.updateSysUser(sysUser);

            return sysUserNeedMapper.updateSysUserNeed(sysUserNeed);
        }

        /*校验手机号*/
        if (sysUser.getPhonenumber() != null && sysUserMapper.checkPhoneNumber(sysUser) == 0) {
            sysUser.setUserName(sysUser.getPhonenumber());
            sysUserMapper.updateSysUser(sysUser);

            return sysUserNeedMapper.updateSysUserNeed(sysUserNeed);
        } else {
            throw new RuntimeException("手机号已存在");
        }

    }

    /**
     * 批量删除需求方
     *
     * @param userIds 需要删除的需求方主键
     * @return 结果
     */
    @Override
    public int deleteSysUserNeedByUserIds(Long[] userIds)
    {
//        sysUserMapper.deleteSysUserByUserIds(userIds);
        return sysUserMapper.deleteSysUserByUserIds(userIds);
    }

    /**
     * 删除需求方信息
     *
     * @param userId 需求方主键
     * @return 结果
     */
    @Override
    public int deleteSysUserNeedByUserId(Long userId)
    {
        return sysUserNeedMapper.deleteSysUserNeedByUserId(userId);
    }
}
